package top.lidee.common.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.lidee.common.bean.AliPayBean;

import static com.alipay.api.AlipayConstants.*;
import static top.lidee.utils.RSAKeys.PRIVATE_KEY;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/9/26 10:24
 */
@Configuration
public class AlipayClientConfig {


    @Autowired
    private AliPayBean aliPayBean;

    /**
     * 公钥模式加签
     * @return
     * @throws AlipayApiException
     */
    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(aliPayBean.getServerUrl());
        alipayConfig.setAppId(aliPayBean.getAppId());
        alipayConfig.setPrivateKey(aliPayBean.getPrivateKey()); //开发者私钥，由开发者自己生成。
        alipayConfig.setFormat(aliPayBean.getFormat());
        alipayConfig.setCharset(CHARSET_UTF8);
        alipayConfig.setAlipayPublicKey(aliPayBean.getPublicKey());
        alipayConfig.setSignType(SIGN_TYPE_RSA2);
        //构造client
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        return alipayClient;
    }

}
